Public Class ACDataGridViewNumericUpDown
   Inherits NumericUpDown
   Implements IDataGridViewEditingControl

   Private DataGridViewControl As DataGridView
   Private ValueIsChanged As Boolean = False
   Private RowIndexNum As Integer

   Public Sub New()
      MyBase.BorderStyle = Windows.Forms.BorderStyle.None
      'Me.Maximum = 20
      'Me.Minimum = 0
   End Sub

   Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle) Implements System.Windows.Forms.IDataGridViewEditingControl.ApplyCellStyleToEditingControl
      Me.Font = dataGridViewCellStyle.Font
      Me.ForeColor = dataGridViewCellStyle.ForeColor
      Me.BackColor = dataGridViewCellStyle.BackColor
   End Sub

   Public Property EditingControlDataGridView() As System.Windows.Forms.DataGridView Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlDataGridView
      Get
         Return DataGridViewControl
      End Get
      Set(ByVal value As System.Windows.Forms.DataGridView)
         DataGridViewControl = value
      End Set
   End Property

   Public Property EditingControlFormattedValue() As Object Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlFormattedValue
      Get
         Return CType(Me.Value, Decimal)
      End Get
      Set(ByVal value As Object)
         If TypeOf value Is Decimal Then
            Me.Value = value
         End If
      End Set
   End Property

   Public Property EditingControlRowIndex() As Integer Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlRowIndex
      Get
         Return RowIndexNum
      End Get
      Set(ByVal value As Integer)
         RowIndexNum = value
      End Set
   End Property

   Public Property EditingControlValueChanged() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlValueChanged
      Get
         Return ValueIsChanged
      End Get
      Set(ByVal value As Boolean)
         ValueIsChanged = value
      End Set
   End Property

   Public Function EditingControlWantsInputKey(ByVal keyData As System.Windows.Forms.Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlWantsInputKey
      Select Case keyData And Keys.KeyCode
         Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp
            Return True

         Case Else
            Return False

      End Select
   End Function

   Public ReadOnly Property EditingPanelCursor() As System.Windows.Forms.Cursor Implements System.Windows.Forms.IDataGridViewEditingControl.EditingPanelCursor
      Get
         Return MyBase.Cursor
      End Get
   End Property

   Public Function GetEditingControlFormattedValue(ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object Implements System.Windows.Forms.IDataGridViewEditingControl.GetEditingControlFormattedValue
      Return CType(Me.Value, Decimal).ToString
   End Function

   Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements System.Windows.Forms.IDataGridViewEditingControl.PrepareEditingControlForEdit
      'En caso de alguna preparacion antes de la edicion (por confirmar)
   End Sub

   Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.RepositionEditingControlOnValueChange
      Get
         Return False 'Leer mas al respecto
      End Get
   End Property

   Protected Overrides Sub OnValueChanged(ByVal e As System.EventArgs)
      ValueIsChanged = True
      Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
      MyBase.OnValueChanged(e)
   End Sub
End Class

Public Class ACDataGridViewNumericUpDownColumn
   Inherits DataGridViewColumn

   Public Sub New()
      MyBase.New(New ACDataGridViewNumericUpDownCell())
   End Sub

   Public Overrides Property CellTemplate() As System.Windows.Forms.DataGridViewCell
      Get
         Return MyBase.CellTemplate
      End Get
      Set(ByVal value As System.Windows.Forms.DataGridViewCell)
         If (value IsNot Nothing) AndAlso Not value.GetType().IsAssignableFrom(GetType(ACDataGridViewNumericUpDown)) Then
            Throw New InvalidCastException("Debe ser un NumericUpDownCell")
         End If
         MyBase.CellTemplate = value
      End Set
   End Property

End Class

Public Class ACDataGridViewNumericUpDownCell
   Inherits DataGridViewTextBoxCell

   Public Sub New()
      'Me.Style.Format = "00"
   End Sub

   Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle)
      MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)

      Dim ctl As ACDataGridViewNumericUpDown = CType(DataGridView.EditingControl, ACDataGridViewNumericUpDown)
      ctl.Value = CType(IIf(Me.Value Is DBNull.Value, 0, Me.Value), Decimal)
   End Sub

   Public Overrides ReadOnly Property EditType() As System.Type
      Get
         'devuelve el tipo del control de edicion que contiene la celda NumericUpDownCell
         Return GetType(ACDataGridViewNumericUpDown)
      End Get
   End Property

   Public Overrides ReadOnly Property ValueType() As System.Type
      'Devuelve el tipo que contiene el NumericUpDownCell
      Get
         Return GetType(Decimal)
      End Get
   End Property

   Public Overrides ReadOnly Property DefaultNewRowValue() As Object
      Get
         Return 0
      End Get
   End Property
End Class



